package com.msconfig.mvp.view.activity.base;

import android.content.Context;

import java.lang.ref.WeakReference;


/*
 * author: caojunhao
 * date  : 2020/4/28
 * desc  :
 */
public class BasePresenter<V extends BaseView> {
    protected WeakReference<V> mViewReference;
    protected Context mContext;

    public BasePresenter(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 绑定view，一般在初始化中调用该方法
     *
     * @param view view
     */
    public void attachView(V view) {
        mViewReference = new WeakReference<V>(view);
    }

    /**
     * 解除绑定view，一般在onDestroy中调用
     */

    public void detachView() {
        if (mViewReference != null) {
            mViewReference.clear();
            mViewReference = null;
        }
    }

    public V getView() {
        if (mViewReference != null) {
            return mViewReference.get();
        }
        return null;
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * View是否绑定
     *
     * @return
     */
    public boolean isViewAttached() {
        return mViewReference != null && mViewReference.get() != null;
    }

}
